<style>
#RegisterDiv{
	text-align: center;
}
#RegisterPage{
	padding: 16px;
	margin: 12vh 1vw;
	border-radius: 2px;
	background: #AAA;
	display: inline-block;
	box-shadow: 2px 2px 3px #343;
}
#RegisterButton{
	width: 100%;
	margin-right: 4px;
	padding-left: 1px;
	padding-right: 1px;
	font-size: 0.95rem;
	margin-bottom: 8px;
	box-sizing: border-box;
}
#RegisterTable td{
	padding: 10px;
	text-align: left;
}
#RegisterTable input{
	padding: 6px;
}
#TitleLogoDiv{
	color: #555;
	font-size: 1.6rem;
	font-weight: bold;
	margin-bottom: 16px;
}
#AgreementLink{
	text-decoration: none;
}
#AgreementLabel{
	margin-top: 0px;
	margin-left: 4px;
	position: absolute;
	display: inline-block;
}
#RegSendMailButton{
	float: right;
	background: none;
	box-shadow: none;
	padding-right: 1px;
	border: 1px solid rgba(0,0,0,0);
}
#RegSendMailButton:hover{
	color: #998822;
}
#RegSendMailButton:active{
	color: #223344;
}
</style>

<div id='RegisterDiv'>
	<div id='RegisterPage'>
		<div id='TitleLogoDiv'>用户注册</div>
		<table id='RegisterTable'>
			<tr><td><v-text id='RegUserText' title='用户名称' maxlength='32' size='28'></v-text></td></tr>
			<tr><td><v-text id='RegMailText' title='关联邮箱' maxlength='128' size='28'></v-text></td></tr>
			<tr><td><v-text id='RegCheckCodeText' title='邮箱验证' maxlength='6' size='8'></v-text><v-button id='RegSendMailButton' title='发送验证邮件'></v-button></td></tr>
			<tr><td><v-text id='RegUserPasswdText' title='登录密码' type='password' maxlength='32' size='28'></v-text></td></tr>
			<tr><td><v-text id='RegCheckPasswdText' title='确认密码' type='password' maxlength='32' size='28'></v-text></td></tr>
			<tr><td><v-button id='RegisterButton' title='立即注册'></v-button></td></tr>
			<tr>
				<td>
					<input id='AgreementCheckbox' type='Checkbox' checked></input>
					<span id='AgreementLabel'>我已阅读并同意<a id='AgreementLink' target='_blank' href='/sharenote?title=AGREEMENT'>《用户注册协议》<a></span>
				</td>
			</tr>
		</table>
	</div>
</div>

<script>
var curmail = null;

getVue('RegisterPage');

$('#RegSendMailButton').click(function(){
	var mail = $('#RegMailText').val();
	
	if (strlen(mail) == 0){
		$('#RegMailText').focus();
	}
	else if (checkEmail(mail)){
		getHttpResult("/SendMail", {flag: 'R', mail: mail}, function(data){
			if (data.code >= 0){
				var sec = 60;
				var txt = $('#RegMailText');
				var btn = $('#RegSendMailButton');
				
				$('#RegCheckCodeText').val('');
				txt.attr('disabled', true);
				btn.attr('disabled', true);
				btn.val(sec + '秒后可重试');
				curmail = mail;
				
				var timer = setInterval(function(){
					if (--sec <= 0){
						txt.removeAttr('disabled');
						btn.removeAttr('disabled');
						btn.val('重新发送邮件');
						clearInterval(timer);
					}
					else{
						txt.attr('disabled', true);
						btn.attr('disabled', true);
						btn.val(sec + '秒后可重试');
					}
					
				}, 1000);
			}
			else if (data.code == XG_DUPLICATE){
				showToast('该邮箱已经注册');
			}
			else{
				showToast("发送频繁(请稍后再试)");
			}
		});
	}
	else{
		showToast('邮箱地址格式错误');
	}
});

$('#RegisterButton').click(function(){
	var user = $('#RegUserText').val();
	var mail = $('#RegMailText').val();
	var code = $('#RegCheckCodeText').val();
	var passwd = $('#RegUserPasswdText').val();
	var chkpwd = $('#RegCheckPasswdText').val();

	if (strlen(user) == 0){
		$('#RegUserText').focus();
	}
	else if (strlen(mail) == 0){
		$('#RegMailText').focus();
	}
	else if (strlen(curmail) == 0){
		showToast('请验证关联邮箱');
	}
	else if (mail != curmail){
		showToast('请重新验证关联邮箱');
	}
	else if (strlen(code) == 0){
		$('#RegCheckCodeText').focus();
	}
	else if (strlen(passwd) == 0){
		$('#RegUserPasswdText').focus();
	}
	else if (strlen(chkpwd) == 0){
		$('#RegCheckPasswdText').focus();
	}
	else if (passwd != chkpwd){
		showToast('两次输入密码不一致');
	}
	else if (!$('#AgreementCheckbox').is(':checked')){
		showToast("请先阅读并同意用户注册协议");
	}
	else{
		var param = {};
		
		param["flag"] = 'R';
		param["user"] = user;
		param["mail"] = mail;
		param["code"] = code;
		param["passwd"] = passwd;
		
		getHttpResult('/EditUser', param, function(data){
			if (data.code == XG_DUPLICATE){
				showToast('用户已经存在');
			}
			else if (data.code == XG_AUTHFAIL){
				showToast('请输入正确的邮箱验证码');
				$('#RegCheckCodeText').focus();
			}
			else if (data.code < 0){
				showToast('注册用户失败');
			}
			else{
				showToast('注册用户成功');

				setTimeout(function(){
					window.history.back();
				}, 1000);
			}
		});
	}
});
</script>


